package com.project.common.tag;

import java.io.Writer;
import java.util.List;

import javax.servlet.http.HttpServletRequest;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.apache.struts2.components.Component;

import com.opensymphony.xwork2.util.ValueStack;
import com.project.common.MemoryConstants;
import com.project.service.front.preSchool.INewsTitleTagService;
import com.project.service.front.preSchool.IPreSchoolService;
import com.project.service.impl.front.preSchool.NewsTitleTagServiceImpl;
import com.project.webapp.form.BaseForm;
import com.project.webapp.form.front.preSchool.ArticleForm;
import com.project.common.util.StringUtil;
/**
 * 构建分页标签体的类
 */
public class NewsTitle extends Component 
{
	public Log log = LogFactory.getLog(this.getClass());
	
	private HttpServletRequest request;
	// 属于菜单的menuId
	private String menuId = null;
	// list显示几笔
	private String recordCount = null;
	// 课程显示的div层1-24
	private String count = null;
	// 类别，course:课程
	private String type = null;
	
	private IPreSchoolService preSchoolService = null;

	// ---------constructor start---------
	// 首先执行无参构造方法，然后执行set方法，在执行start方法
	public NewsTitle(ValueStack vs, HttpServletRequest request) 
	{
		super(vs);
		this.request = request;
	}

	// ---------constructor end---------
	@Override
	public boolean start(Writer writer) 
	{
		boolean result = super.start(writer);
		try 
		{
			StringBuffer str = new StringBuffer();

			INewsTitleTagService newsTitleTagService = new NewsTitleTagServiceImpl();
			ArticleForm artForm = new ArticleForm();
			artForm.setMenuId(menuId);
			artForm.setCount(count);// 第几层
			artForm.setRecordCount(recordCount);// 显示第几条数据
			artForm.setType(type);

			List<BaseForm> list = newsTitleTagService.query(artForm, log,
					MemoryConstants.PRE_SCHOOL_HOME_COURSE_NEWS + count, 0,
					request.getSession().getServletContext());		

			ArticleForm art = null;
					
			// 用于确认是否是首条新闻，默认的是true，当显示第一条之后，将值之为false
			boolean isFirstLine = true;
			// 实现跟踪list中元素的位置
			int i = 0;
			//用来跟踪显示新闻的位置，是否需要显示图片信息4,5,6,7 13,14,15,16  21,22,23,24
			int temp = Integer.parseInt(artForm.getCount());
			// 用来页面标签中传入的是需要显示几条新闻
			int count = Integer.parseInt(artForm.getRecordCount());
			for (BaseForm bf : list) 
			{
				i++;// 每遍历一次都加1
				art = (ArticleForm) bf;// 强制转换
				// 用于显示左边的新闻
				if (temp == 1 || temp == 2 || temp == 3 || temp == 4
						|| temp == 9 || temp == 10 || temp == 11 || temp == 12
						|| temp == 17 || temp == 18 || temp == 19 || temp == 20) 
				{
					// 这边显示的是左边的新闻，不带图片的新闻展示功能
					if (isFirstLine) 
					{
						// 防止标题的文字过长显示
						String	title=new StringUtil().substringUTF8(art.getArticle_title(), 44);
						str.append(
								"<strong>&nbsp;<a target='_blank' href=\"readnew.action?id="
										+ art.getArticle_id() + "\">")
								.append(title).append("</a> </strong>");
						isFirstLine = false;
						// 第六条内容和其他的样式不一致，我们需要特别判断一下，在进行输出显示新闻
					} else if (i == 6) 
					{
						String	title=new StringUtil().substringUTF8(art.getArticle_title(), 44);
						str.append(
								"<li class='mt10'><a href='readnew.action?id="
										+ art.getArticle_id()
										+ "' class=\"red\">【")
								.append(art.getMenu_name())
								.append("】</a><a target='_blank' href='readnew.action?id="
										+ art.getArticle_id() + "'"
										+ art.getArticle_id() + "\">")
								.append(title).append("</a></li>");
					} else 
					{
						String	title=new StringUtil().substringUTF8(art.getArticle_title(), 44);
						str.append(
								"<li><a target='_blank' href='readnew.action?id="
										+ art.getArticle_id()
										+ "' class=\"red\">【")
								.append(art.getMenu_name())
								.append("】</a><a target='_blank' href='readnew.action?id="
										+ art.getArticle_id() + "'"
										+ art.getArticle_id() + "\">")
								.append(title).append("</a></li>");

					}
					// 用于显示左边的新闻
				} else if (temp == 5 || temp == 6 || temp == 7 || temp == 8
						|| temp == 13 || temp == 14 || temp == 15 || temp == 16
						|| temp == 21 || temp == 22 || temp == 23 || temp == 24) 
				{

					// 判断是否是首条新闻
					if (isFirstLine) 
					{
						// 取出文章的标题，防止标题的长度过长
						String title = art.getArticle_title().length() < 24
								&& art.getArticle_title().length() > 0 ? art
								.getArticle_title() : art.getArticle_title()
								.substring(0, 20);
						// 取出文章的内容
						String content = art.getArticle_content().substring(3,
								art.getArticle_content().length() - 6);
						str.append(
								"<div class='Courses_Art'><strong class='w359'>&nbsp;<a target='_blank' href='readnew.action?id=")
								.append(art.getArticle_id() + "'>")
								.append(title)
								.append("</a></strong>")
								.append("<p class='left'><img width='122' height='86' src='http://www.pudug.com/front/preSchool/style/image/course_img1.png")
								.append(art.getPic_link() == null ? "course_img1.png"
										: art.getPic_link())
								.append("'/></p><p class='Courses_Art_font left'>")
								.append(""
										+ (content.length() > 55 ? content
												.substring(0, 54) : content))
								.append("<a target='_blank' href='readnew.action?id="
										+ art.getArticle_id()
										+ "'>[详细]</a></p></div>")
								.append("<div class='Courses_Art_list w359 mt10'><ul>");
						isFirstLine = false;
					} else 
					{
						// 显示第二条新闻之后的东西
						String title = new StringUtil().substringDBCS(art.getArticle_title(), 38, "utf-8");
						str.append("<li><a target='_balnk' href='readnew.action?id="
								+ art.getArticle_id() + "' class='red'>【"
								+ art.getMenu_name()
								+ "】</a><a href='readnew.action?id="
								+ art.getArticle_id() + "'>" + title
								+ "</a></li>");
					}
					if (i == count) 
					{
						str.append("</ul><div class='Vocal_title_more mt10'><a href='#'>更多》</a></div></div>");
					}
				}

			}
			//用于填充数据没有完整的情况下的填充
			if(count!=list.size())
			{
				// 用于判断页面的层次，左边
				if (temp == 1 || temp == 2 || temp == 3 || temp == 4 || temp == 9
						|| temp == 10 || temp == 11 || temp == 12 || temp == 17
						|| temp == 18 || temp == 19 || temp == 20) 
				{
					if (i == 0) 
					{
						i++;
						str.append("<strong>&nbsp;<a href=\"#\">")
						.append("该类栏目下还没有对应的新闻").append("</a> </strong>");
					}
					for (int j = i; j < count; j++) 
					{
						i++;
						if (i == 6) 
						{
							str.append(
									"<li class='mt10'><a href=\"#\" class=\"red\">【")
									.append("普度").append("】</a><a href=\"#\">")
									.append("该类栏目下还没有对应的新闻").append("</a></li>");
						} else 
						{
							str.append("<li><a href=\"#\" class=\"red\">【")
							.append("普度").append("】</a><a href=\"#\">")
							.append("该类栏目下还没有对应的新闻").append("</a></li>");
						}
					}
					
				}
				// 右边显示
				else if (temp == 5 || temp == 6 || temp == 7 || temp == 8
						|| temp == 13 || temp == 14 || temp == 15 || temp == 16
						|| temp == 21 || temp == 22 || temp == 23 || temp == 24) 
				{
					if (i == 0) 
					{
						i++;
						str.append("<div class='Courses_Art'><strong>&nbsp;<a href='#'>无效赴英留学生被强制面试 印巴受影响大</a></strong><p class='left'><img src='http://192.168.1.101:8080/pudu/front/preSchool/style/image/course_img1.png' width='122' height='86' /></p><p class='Courses_Art_font left'> 河南商报讯（记者 吴静）生志愿被偷改。记者从省古筝古筝招办获悉，该案件已查清，一考愿，当前省招办已帮其恢息。<a target='_blank' href='#'>[详细]</a></p></div><div class='Courses_Art_list w359 mt10'><ul>");
					}
					if (i < count) 
					{
						for (int j = i; j < count; j++) 
						{
							str.append("<li><a href='#' class='red'>【普度】</a><a href='#'>武汉最牛艺考生高考670分弃北大清华报</a></li>");
							if (j == count - 1) 
							{
								str.append("</ul><div class='Vocal_title_more mt10'><a href='#'>更多》</a></div></div>");
							}
						}
					}
				}
			}
			if(temp==2)
			{
				
				System.out.println("str："+str.toString());
			}
			
			writer.write(str.toString());

		} catch (Exception e) 
		{
			e.printStackTrace();
		}
		return result;
	}

	// ---------setter & getter start---------
	public String getMenuId() {
		return menuId;
	}

	public void setMenuId(String menuId) {
		this.menuId = menuId;
	}

	public String getCount() {
		return count;
	}

	public void setCount(String count) {
		this.count = count;
	}

	public IPreSchoolService getPreSchoolService() {
		return preSchoolService;
	}

	public void setPreSchoolService(IPreSchoolService preSchoolService) {
		this.preSchoolService = preSchoolService;
	}

	public String getRecordCount() {
		return recordCount;
	}

	public void setRecordCount(String recordCount) {
		this.recordCount = recordCount;
	}

	public String getType() {
		return type;
	}

	public void setType(String type) {
		this.type = type;
	}

	// ---------setter & getter end---------
}
